<template>
  <!--基本信息  -->
  <div>
    <FormPanel ref="formPanel" :list="initForm" :cols="2" :formType="type" :labelWidth="120" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" :loading="saveLoading" @click="saveDialog" type="primary">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { updateFollowInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addFollowInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { getUser } from '@/assets/js/auth'; //获取登录用户
export default {
  mixins: [authority, dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      followId: this.rowData['followId'],
      projectId: this.rowData['projectId'],
      saveLoading: false,
      initForm: this.initFormFun(),
      formValue: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  mounted() {
    let user;
    user = getUser();

    this.initForm.find(item => item.fieldName === 'followPeople').initialValue = user;
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log(this.type);
      if (this.type === 'edit') {
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }

      var myDate = new Date(); //Thu Dec 28 2017 10:</span>07:39 GMT+0800 (中国标准时间)
      var month = myDate.getMonth() + 1;
      var date = myDate.getDate();
      if (month < 10) {
        month = '0' + month;
      }
      if (date < 10) {
        date = '0' + date;
      }

      var str = myDate.getFullYear() + '-' + month + '-' + date + ' 00:00:00'; //2017-12-28
      this.initForm.find(item => item.fieldName === 'followDate').initialValue = str;
    },
    initFormFun() {
      return [
        {
          type: 'DATE',
          label: '跟进日期:',
          fieldName: 'followDate',
          valueFormat: 'yyyy-MM-dd',
          rules: [{ required: true, message: '请填写跟进日期', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '跟进方式',
          fieldName: 'followType',
          change: this.isChange,
          placeholder: '请选择',
          itemList: this.createDictList('2519'),
          rules: [{ required: true, message: '请填写跟进方式', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '客户状态',
          fieldName: 'customerStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2507'),
          rules: [{ required: true, message: '请填写客户状态', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '本次跟进结果',
          fieldName: 'followStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2520'),
          rules: [{ required: true, message: '请填写跟进状态', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '客户关系进度',
          fieldName: 'relationProgress',
          placeholder: '请选择',
          itemList: this.createDictList('2521'),
          rules: [{ required: true, message: '请填写客户关系进度', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '会见人:',
          fieldName: 'meetingPeople',
          disabled: false,
          hidden: false,
          rules: [{ max: 20, message: '会见人长度不能超过20', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '成交机会率(%):',
          fieldName: 'dealRate',
          disabled: false,
          // rules: [{ pattern: /^0\.[0-9]{1,2}$|^0{1}$|^1{1}$|^1\.[0]{1,2}$/, message: '请填写0-1之间的数字,并保留两位小数', trigger: 'change' }]
          rules: [{ required: true, pattern: /^(100|[1-9]?\d(\.\d\d?)?)$/, message: '0-100的数字,最多两位小数', trigger: 'change' }]
        },
        // {
        //   type: 'DATE',
        //   label: '计划跟进日期:',
        //   fieldName: 'planDate',
        //   valueFormat: 'yyyy-MM-dd HH:mm:ss',
        //   rules: [{ required: true, message: '请填写计划跟进日期', trigger: 'blur' }]
        // },
        {
          type: 'DATE',
          label: '预计下次跟进日期:',
          fieldName: 'planNextDate',
          valueFormat: 'yyyy-MM-dd',
          rules: [{ required: true, message: '请填写预计下次跟进日期', trigger: 'blur' }]
        },
        // {
        //   type: 'INPUT',
        //   label: '同行人:',
        //   fieldName: 'togetherPeople',
        //   disabled: false,
        //   rules: [{ max: 20, message: '同行人长度不能超过20', trigger: 'blur' }]
        // },

        {
          type: 'INPUT',
          label: '跟进人:',
          fieldName: 'followPeople',
          disabled: false,
          rules: [
            { required: true, message: '请填写跟进人', trigger: 'blur' },
            { max: 20, message: '跟进人长度不能超过20', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '输出成果:',
          fieldName: 'outputResult',
          disabled: false,
          selfCols: 2,
          rules: [{ max: 100, message: '输出成果长度不能超过100', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '下次目标:',
          fieldName: 'nextTarget',
          disabled: false,
          selfCols: 2,
          rules: [{ max: 100, message: '下次目标长度不能超过100', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '备注:',
          fieldName: 'remark',
          disabled: false,
          selfCols: 2,
          rules: [{ max: 200, message: '备注长度不能超过200', trigger: 'blur' }]
        }
      ];
    },

    async formChangeHandle(val) {
      this.formValue = val;
      if (this.formValue !== null) {
        if (new Date(this.formValue.followDate).getTime() > new Date(this.formValue.planNextDate).getTime()) {
          this.$notify.warning({ title: '提示信息', message: '跟进日期不能大于下次跟进日期!' });
          return false;
        }
        this.saveLoading = true;
        console.log('333333----', this.formValue);
        let res = {};

        this.formValue = Object.assign({}, { projectId: this.projectId }, this.formValue);
        console.log('2222----', this.formValue);
        console.log('type', this.type);
        if (this.type === 'add') {
          res = await addFollowInfo(this.formValue);
        }
        console.log('add', res);
        if (this.type === 'edit') {
          res = await updateFollowInfo(this.formValue, this.followId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        } else {
          this.closeDialog('refresh');
          this.$notify.warning({ title: '提示信息', message: res.errMsg });
        }
      }
    },
    saveDialog() {
      console.log('select');
      this.$refs.formPanel.SUBMIT_FORM();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    isChange(val) {
      console.log(val);
      if (val == 25191003) {
        //上门拜访显示 会见人

        this.initForm[1].initialValue = val;
        this.initForm[5].hidden = false;
      } else {
        //隐藏会见人
        this.initForm[1].initialValue = val;
        this.initForm[5].hidden = true;
      }
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
